﻿using DevExpress.XtraEditors;
using SmartDisplayControl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CamHKVision
{
    public partial class XtraFormSetting : DevExpress.XtraEditors.XtraForm
    {
        public string strName;
        public CamHKVision camGeneral;
        public CamHKVisionActioner camera;
        public XtraFormSetting()
        {
            InitializeComponent();
        }

        private void XtraFormSetting_Load(object sender, EventArgs e)
        {
            labelControlCameraName.Text = camGeneral.strName;
            propertyGridControl1.SelectedObject = this.camGeneral.camGeneralSetting;
            camera = (CamHKVisionActioner)camGeneral.actioner;
            camera.SetGainAndShutter(camGeneral.camGeneralSetting.Gain, camGeneral.camGeneralSetting.ExporsureMs);
            textEdit曝光时间.Text= camera.GetShutter().ToString();
            textEdit增益.Text= camera.GetGain().ToString();
        }

        private void propertyGridControl1_CellValueChanged(object sender, DevExpress.XtraVerticalGrid.Events.CellValueChangedEventArgs e)
        {
            
            camera.SetGainAndShutter(camGeneral.camGeneralSetting.Gain, camGeneral.camGeneralSetting.ExporsureMs);
            textEdit曝光时间.Text = camera.GetShutter().ToString();
            textEdit增益.Text = camera.GetGain().ToString();
        }

        private void simpleButtonGrab_Click(object sender, EventArgs e)
        {
            StopGetFlyImage();
            HalconDotNet.HImage hImage1 = new HalconDotNet.HImage(camera.GrabImage());
            smartDisplayControl1.ShowImage(hImage1);
        }
        public bool StopGetFlyImage()
        {
            camera.StopFlyGrab();
            return true;
        }
        bool bStartLive;
        Task taskLive;
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (bStartLive)
                return;
            bStartLive = true;
            simpleButtonGrab.Enabled = false;
            simpleButton2.Enabled = false;
            taskLive =Task.Factory.StartNew(() =>
            {
                HalconDotNet.HImage hImage1 = null;
                StopGetFlyImage();
                while (bStartLive)
                {
                    System.Threading.Thread.Sleep(20);
                    if (hImage1 != null)
                    {
                        hImage1.Dispose();
                    }
                    hImage1 = camera.GrabImage();
                    smartDisplayControl1.ShowImage(hImage1);
                }

            });
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            bStartLive = false;
            simpleButtonGrab.Enabled = true;
            simpleButton2.Enabled = true;
        }

        private void XtraFormSetting_FormClosing(object sender, FormClosingEventArgs e)
        {
            bStartLive = false;
            if (taskLive != null)
            {
                taskLive.Wait();
            }
        }
    }
}